package com.hwhhhh.graduationproject.controller;

import com.hwhhhh.graduationproject.controller.param.UserReq;
import com.hwhhhh.graduationproject.annotation.CurrentUser;
import com.hwhhhh.graduationproject.domain.model.User;
import com.hwhhhh.graduationproject.exception.InvalidAccountException;
import com.hwhhhh.graduationproject.utils.ImageUtils;
import com.hwhhhh.graduationproject.utils.JWTUtils;
import com.hwhhhh.graduationproject.utils.ResponseCode;
import com.hwhhhh.graduationproject.utils.ResponseData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description detail
 * Created by Hwhhhh on 2021/3/2 15:08
 */
@RestController
@Slf4j
public class UserController extends BaseController{
    @PostMapping("/users/login")
    public ResponseData login(@RequestBody UserReq userReq) {
        User user;
        try {
           user = this.userService.login(userReq);
        } catch (NullPointerException e) {
            return new ResponseData(ResponseCode.WRONG_LOGIN);  // 用户名或密码错误
        } catch (InvalidAccountException e) {
            return new ResponseData(ResponseCode.INVALID_ACCOUNT);  // 用户已被封禁（如何避免已被封禁的用户访问其他的api)
        } catch (Exception e) {
            return new ResponseData(ResponseCode.SERVER_ERROR);
        }
        // 放入payload
        Map<String, String> payload = new HashMap<>();
        payload.put("id", Integer.toString(user.getId()));
        payload.put("isAd", Integer.toString(user.getIsAd()));
        // 生成令牌
        String token = JWTUtils.generateToken(payload);
        user.setToken(token);
        return new ResponseData(ResponseCode.SUCCESS, user);
    }

    @PostMapping("/users/register")
    public ResponseData register(@RequestBody UserReq userReq) {
        if (!this.userService.checkEmail(userReq.getEmail())) {   // 检验是否已存在email
            if (userReq.getEmail() != null && userReq.getPassword() != null) this.userService.register(userReq);
        } else {
            return new ResponseData(ResponseCode.DUPLICATE_EMAIL);  // 邮箱重复
        }
        return new ResponseData(ResponseCode.SUCCESS);
    }

    @PutMapping("/update/users")
    public ResponseData updateInfo(@RequestBody UserReq userReq, @CurrentUser User user) {
        try {
            this.userService.updateUserInfo(userReq, user.getId());
        } catch (Exception e) {
            return new ResponseData(ResponseCode.SERVER_ERROR);
        }
        return new ResponseData(ResponseCode.SUCCESS);
    }

    @PostMapping("/images/users")
    public ResponseData uploadProfilePhoto(MultipartFile file, @CurrentUser User user) {
        try {
            //先保存用户头像，返回对应url
            String url = ImageUtils.uploadImage(file, ImageUtils.USER);
            // 将url保存至对应user表中
            this.userService.uploadProfilePhoto(user.getId(), url);
            return new ResponseData(ResponseCode.SUCCESS, url);
        } catch (IOException e) {
            e.printStackTrace();
            return new ResponseData(ResponseCode.HEADPORTRAIT_ERROR);   // 上传头像出错
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseData(ResponseCode.SERVER_ERROR);
        }
    }

}
